package org.mogoo.simple.socket2;

import javax.swing.plaf.basic.BasicInternalFrameTitlePane;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;

/**
 * Created with IntelliJ IDEA.
 * Author: Fang
 * Date: 2012-08-21
 * Time: 19:48
 * Email: dongfang@mogoo.org
 */
public class Server
{
    //private Socket socket = null;
    private ServerSocket serverSocket = null;
    private static HashMap socketMap = new HashMap();


    public static void main(String [] args) throws Exception
    {
        new Server().startServer();

    }

    public void startServer() throws Exception
    {
        serverSocket = new ServerSocket(8888);

        while(true)
        {
            Socket socket = serverSocket.accept();
            Thread t = new Thread(new SocketSelection(socket));
            t.start();
        }
    }

    class SocketSelection implements Runnable
    {
        Socket socket = null;

        public SocketSelection(Socket socket)
        {
            this.socket = socket;
        }

        public void run()
        {
            try
            {
                ObjectInputStream objis = new ObjectInputStream(socket.getInputStream());
                RequestBody requestBody = (RequestBody)objis.readObject();
                RequestType requestType = requestBody.getRequestType();
                System.out.println("server: object is " + requestBody);
                switch(requestType)
                {
                    case login:
                        try
                        {
                            handleLogin(requestBody,socket);
                        }
                        catch(Exception e)
                        {
                            e.printStackTrace();
                        }
                        break;
                    case logout:
                        try
                        {
                            handleLogout(requestBody);
                        }
                        catch(Exception e)
                        {
                            e.printStackTrace();
                        }
                        break;
                    case message:
                        try
                        {
                            handleMessage(requestBody);
                        }
                        catch(Exception e)
                        {
                            e.printStackTrace();
                        }
                        break;
                }
            }
            catch(IOException e)
            {
                e.printStackTrace();
            }
            catch(ClassNotFoundException e)
            {
                e.printStackTrace();
            }
        }
    }

    public void handleMessage(RequestBody requestBody) throws Exception
    {
        Socket socket = (Socket)socketMap.get(requestBody.getDestinationID());
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
        objectOutputStream.writeObject(requestBody);

    }

    public void handleLogin(RequestBody requestBody, Socket socket) throws Exception
    {
        socketMap.put(requestBody.getClientID(), socket);
        RequestBody requestBody1 = new RequestBody();
        requestBody1.setDestinationID(2);
        requestBody1.setMessageBody("server");
        ObjectOutputStream ob = new ObjectOutputStream(socket.getOutputStream());
        ob.writeObject(requestBody1);
        System.out.println("server: client " + requestBody.getClientID() + " login.");

    }

    public void handleLogout(RequestBody requestBody) throws Exception
    {
        Socket socket = (Socket)socketMap.get(requestBody.getClientID());
        socketMap.remove(requestBody.getClientID());
        PrintStream ps = new PrintStream(socket.getOutputStream());
        ps.println("You are now login!");
        ps.close();
        socket.close();
    }


}
